package com.cskaoyan.controller;


import com.cskaoyan.annotation.ExecutionCreateLog;
import com.cskaoyan.annotation.ExecutionLog;
import com.cskaoyan.bean.bo.WxglMallManagementControllerBo.IssueCreateBo;
import com.cskaoyan.bean.bo.WxglMallManagementControllerBo.KeywordListBo;
import com.cskaoyan.bean.bo.WxglMallManagementControllerBo.OrderListBo;
import com.cskaoyan.bean.bo.WxglMallManagementControllerBo.PostPermissionBo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.marketBean.*;
import com.cskaoyan.bean.vo.WxglMallManagementControllerVo.*;
import com.cskaoyan.service.WxglMarketManagementService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author wxgl
 * @date 2022-12-28 16:47
 * @describe
 */

@RequestMapping("admin")
@RestController
public class WxglMallManagementController {

    @Autowired
    WxglMarketManagementService wxglMarketManagementService;
    /**
     * @Author xiaowu
     * @Description //TODO http://182.92.235.201:8083/admin/order/list?page=1&limit=20&sort=add_time&order=desc&start=&end=
     * @Date 16:52 2022/12/28
     * @Param 
     * @return {errno: 0, data: {total: 268, pages: 14, limit: 20, page: 1,…}, errmsg: "成功"}
     **/

    @RequestMapping("order/list")
    public BaseRespVo orderList(OrderListBo orderListBo){
        Subject subject = SecurityUtils.getSubject();
        MarketAdmin principal = (MarketAdmin) subject.getPrincipal();
        Integer id = principal.getId();
        System.out.println("id = " + id);
        CommonData<OrderListVo> data = wxglMarketManagementService.orderList(orderListBo);
        return BaseRespVo.success(data);
    }

    @RequestMapping("order/channel")
    public BaseRespVo<MarketChannel> orderChannel(){
        List<MarketChannel> data = wxglMarketManagementService.orderChannel();
        return BaseRespVo.success(data);
    }

    /**
     * @Author xiaowu
     * @Description //TODO
     * @Date 23:33 2022/12/28
     * @Param [] 请求 URL http://182.92.235.201:8083/admin/order/detail?id=314
     * @return com.cskaoyan.bean.common.BaseRespVo<com.cskaoyan.bean.vo.WxglMallManagementControllerVo.OrderDetailVo>
     **/

    @RequestMapping("order/detail")
    public BaseRespVo<OrderDetailVo> orderDetail(Integer id){
        OrderDetailVo data = wxglMarketManagementService.orderDetail(id);
        return BaseRespVo.success(data);
    }
    
    /**
     * @Author xiaowu
     * @Description //TODO 发货
     * @Date 0:12 2022/12/29
     * @Param http://localhost:8083/admin/order/ship
     *   {"orderId":19,"shipChannel":"ZTO","shipSn":"20220716593322"}
     * @return  {"errno":0,"errmsg":"成功"}
     **/
    @ExecutionCreateLog
    @RequestMapping("order/ship")
    public BaseRespVo orderShip(@RequestBody Map map){
        int code = wxglMarketManagementService.orderShip(map);
        if (code == 10000){
            return BaseRespVo.invalidData("发货失败");
        }
        return BaseRespVo.success(null);
    }

    /**
     * @Author xiaowu
     * @Description //TODO 删除订单信息，其实就是把订单和货物订单中的deleted属性改为1
     * @Date 0:39 2022/12/29
     * @Param {"orderId":19}
     * @return {"errno":623,"errmsg":"订单不能删除"}  订单状态不是用户收货的状态 是不可以删除的
     * {"errno":0,"errmsg":"成功"}
     **/

    @RequestMapping("order/delete")
    public BaseRespVo orderDelete(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        int code = wxglMarketManagementService.orderDelete(orderId);
        if (code ==200){
            return BaseRespVo.success(null);
        }
        return new BaseRespVo(null,"订单不能删除",623);
    }

    /**
     * @Author xiaowu
     * @Description //TODO 退款
     * @Date 14:01 2022/12/29
     * @Param {"orderId":308,"refundMoney":383}
     * @return {"errno":0,"errmsg":"成功"}
     **/

    @RequestMapping("order/refund")
    public BaseRespVo orderRefund(@RequestBody Map map){
        wxglMarketManagementService.orderRefund(map);
        return BaseRespVo.success(null);
    }
    
    /**
     * @Author xiaowu
     * @Description //TODO 显示所有的通用问题
     * @Date 8:35 2022/12/29
     * @Param http://localhost:8083/admin/issue/list?page=1&limit=20&sort=add_time&order=desc
     * @return {"errno":0,"data":{"total":3,"pages":1,"limit":20,"page":1,"list":[{"id":19,"question":"112","answer":"111","addTime":"2022-11-22 10:30:02","updateTime":"2022-11-22 10:31:11","deleted":false},{"id":10,"question":"测试13","answer":"为什么111","addTime":"2022-11-21 14:34:04","updateTime":"2022-11-21 15:28:28","deleted":false},{"id":9,"question":"测试11","answer":"测试answer22","addTime":"2022-11-20 21:59:45","updateTime":"2022-11-20 22:31:16","deleted":false}]},"errmsg":"成功"}
     **/

    @RequestMapping("issue/list")
    public BaseRespVo issueList(@Valid BasePageInfo basePageInfo,String question){
        CommonData<ListEntityVo> data = wxglMarketManagementService.issueList(basePageInfo,question);
        return BaseRespVo.success(data);
    }

    /**
     * @Author xiaowu
     * @Description //TODO 添加通用问题
     * @Date 10:12 2022/12/29
     * @Param {"question":"一般下单多久之后发货","answer":"24小时内哦 亲"}
     * @return {"errno":0,"data":{"id":25,"question":"一般多久发货","answer":"24小时内哦 亲","addTime":"2022-12-29 10:13:01","updateTime":"2022-12-29 10:13:01"},"errmsg":"成功"}
     **/

    @ExecutionLog
    @RequestMapping("issue/create")
    public BaseRespVo issueCreat(@RequestBody @Valid IssueCreateBo issueCreateBo, BindingResult bindingResult){
        IssueCreateVo issueCreateVo = wxglMarketManagementService.issueCreat(issueCreateBo);
        return BaseRespVo.success(issueCreateVo);
    }
    
    /**
     * @Author xiaowu
     * @Description //TODO 修改通用问题
     * @Date 10:51 2022/12/29
     * @Param {"id":25,"question":"一般多久发货呢呢","answer":"24小时内哦 亲","addTime":"2022-12-29 10:13:01","updateTime":"2022-12-29 10:13:01","deleted":false}
     *
     * @return {"errno":0,"data":{"id":25,"question":"一般多久发货呢呢","answer":"24小时内哦 亲","addTime":"2022-12-29 10:13:01","updateTime":"2022-12-29 10:56:31","deleted":false},"errmsg":"成功"}
     **/

    @ExecutionLog
    @RequestMapping("issue/update")
    public BaseRespVo issueUpdate(@RequestBody @Valid MarketIssue marketIssue,BindingResult bindingResult){
        MarketIssue marketIssue1 = wxglMarketManagementService.issueUpdate(marketIssue);
        return BaseRespVo.success(marketIssue1);
    }
    
    /**
     * @Author xiaowu
     * @Description //TODO 删除通用问题
     * @Date 11:43 2022/12/29
     * @Param {"id":25,"question":"一般多久发货呢呢1","answer":"24小时内哦 亲","addTime":"2022-12-29 10:13:01","updateTime":"2022-12-29 11:05:02","deleted":false}
     * @return {"errno":0,"errmsg":"成功"}
     **/

    @RequestMapping("issue/delete")
    public BaseRespVo issueDelete(@RequestBody MarketIssue marketIssue){
        int code = wxglMarketManagementService.issueDelete(marketIssue);
        if (code == 200){
            return BaseRespVo.success(null);
        }
        return new BaseRespVo(null,"删除失败",10000);
    }
    /**
     * @Author xiaowu
     * @Description //TODO 显示所有的关键词
     * @Date 12:31 2022/12/29
     * @Param http://182.92.235.201:8083/admin/keyword/list?page=1&limit=20&keyword=t&url=1&sort=add_time&order=desc
     * @return {"errno":0,"data":{"total":1,"pages":1,"limit":20,"page":1,"list":[{"id":43,"keyword":"test","url":"1","isHot":true,"isDefault":true,"sortOrder":100,"addTime":"2022-12-28 17:38:01","updateTime":"2022-12-28 17:38:01","deleted":false}]},"errmsg":"成功"}
     **/
    @ExecutionCreateLog
    @RequestMapping("keyword/list")
    public BaseRespVo keywordList(KeywordListBo keywordListBo){
        CommonData<KeywordListVo> data = wxglMarketManagementService.keywordList(keywordListBo);
        return BaseRespVo.success(data);
    }
    
    /**
     * @Author xiaowu
     * @Description //TODO 修改关键词
     * @Date 13:06 2022/12/29
     * @Param {"deleted":false,"addTime":"2018-01-31 16:00:00","sortOrder":100,"updateTime":"2018-01-31 16:00:00","id":1,"keyword":"母亲节1","url":"mother1","default":false,"hot":false,"isHot":true,"isDefault":true}
     * @return {"errno":0,"data":{"id":43,"keyword":"test1","url":"12345","isHot":false,"isDefault":false,"sortOrder":100,"addTime":"2022-12-28 17:38:01","updateTime":"2022-12-29 13:07:27","deleted":false},"errmsg":"成功"}
     **/

    @ExecutionLog
    @RequestMapping("keyword/update")
    public BaseRespVo keywordUpdate(@RequestBody @Valid MarketKeyword marketKeyword,BindingResult bindingResult){
        MarketKeyword marketKeyword1 = wxglMarketManagementService.keywordUpdate(marketKeyword);
        return BaseRespVo.success(marketKeyword1);
    }
    
    /**
     * @Author xiaowu
     * @Description //TODO 添加关键词
     * @Date 13:23 2022/12/29
     * @Param {"keyword":"开心树下开心果","url":"开心树下你和我","isHot":true,"isDefault":true}
     * @return {"errno":0,"data":{"id":44,"keyword":"123321","url":"12322","isHot":true,"isDefault":false,"addTime":"2022-12-29 13:24:17","updateTime":"2022-12-29 13:24:17"},"errmsg":"成功"}
     **/

    @ExecutionLog
    @RequestMapping("keyword/create")
    public BaseRespVo keywordCreate(@RequestBody @Valid MarketKeyword marketKeyword, BindingResult bindingResult){
        MarketKeyword marketKeyword1 = wxglMarketManagementService.keywordCreate(marketKeyword);
        return BaseRespVo.success(marketKeyword1);
    }
    
    /**
     * @Author xiaowu
     * @Description //TODO 删除关键此词
     * @Date 13:48 2022/12/29
     * @Param {"id":11,"keyword":"1111","url":"1111","isHot":false,"isDefault":false,"sortOrder":null,"addTime":"2022-12-29 05:47:07","updateTime":"2022-12-29 05:47:07","deleted":false}
     * @return {"errno":0,"errmsg":"成功"}
     **/

    @RequestMapping("keyword/delete")
    public BaseRespVo keywordDelete(@RequestBody MarketKeyword marketKeyword){
        wxglMarketManagementService.keywordDelete(marketKeyword);
        return BaseRespVo.success(null);
    }

    /**
     * @Author xiaowu
     * @Description //TODO 获取指定角色id的权限信息以及所有的权限信息
     * @Date 16:14 2023/1/1
     * @Param []
     * @return com.cskaoyan.bean.common.BaseRespVo
     **/

    @GetMapping("role/permissions")
    public BaseRespVo rolePermissions(Integer roleId){
        PermissionVo permissionVo = wxglMarketManagementService.rolePermissions(roleId);
        return BaseRespVo.success(permissionVo);
    }

    /**
     * @Author xiaowu
     * @Description //TODO 修改角色权限
     * @Date 20:30 2023/1/1
     * @Param [postPermissionBo]
     * @return com.cskaoyan.bean.common.BaseRespVo
     **/
    @PostMapping("role/permissions")
    public BaseRespVo rolePermissionsPost(@RequestBody PostPermissionBo postPermissionBo){
        wxglMarketManagementService.rolePermissionsPost(postPermissionBo);
        return BaseRespVo.success();
    }
}
